package com.bw.task.http;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class HttpRetrofit2 {
    private static volatile HttpRetrofit2 httpRetrofit;
    private Retrofit retrofit;


    public static HttpRetrofit2 getHttpRetrofit() {
        if (httpRetrofit==null){
            synchronized (HttpRetrofit2.class){
                if (httpRetrofit==null){
                    httpRetrofit=new HttpRetrofit2();
                }
            }
        }
        return httpRetrofit;
    }


    public Retrofit getRetrofit(){
        if (retrofit==null){
            retrofit= new Retrofit.Builder()
                    .client(new OkHttpClient.Builder()
                            .addInterceptor(new MyInterceptor())
                            .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                            .connectTimeout(2, TimeUnit.MINUTES)
                            .writeTimeout(2, TimeUnit.MINUTES)
                            .readTimeout(2,TimeUnit.MINUTES)
                            .build())
                    .baseUrl("http://service.picasso.adesk.com/v1/")
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}
